package edu.lsnu.equipment_management_system.service.impl;

import edu.lsnu.equipment_management_system.exception.DateNullException;
import edu.lsnu.equipment_management_system.mapper.update.AdminReviewMapper;
import edu.lsnu.equipment_management_system.pojo.vo.sql_conditions.DateAndEquipmentNoVo;
import edu.lsnu.equipment_management_system.service.AdminReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author mzy
 * @Description
 * @date 2018/11/25
 */
@Service
@Transactional(propagation= Propagation.REQUIRED)
public class AdminReviewServiceImpl implements AdminReviewService {
    @Autowired
    AdminReviewMapper adminReviewMapper;
    /**
     * @Description 报损报废审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   boolean
     * @params  [equipmentNo]
     */
    @Override
    public boolean damageReview(String equipmentNo) {
        int count = adminReviewMapper.adminDamageReview(equipmentNo);

        return count == 0 ? false : true;
    }

    /**
     * @Description 调入审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   boolean
     * @params  [equipmentNo]
     */
    @Override
    public boolean transferInReview(String equipmentNo) {
        int count = adminReviewMapper.adminTransferInReview(equipmentNo);

        return count == 0 ? false : true;
    }

    /**
     * @Description 调出审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   boolean
     * @params  [equipmentNo]
     */
    @Override
    public boolean transferOutReview(String equipmentNo) {
        int count = adminReviewMapper.adminTransferOutReview(equipmentNo);

        return count == 0 ? false : true;
    }

    /**
     * @Description 购入审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   boolean
     * @params  [equipmentNo]
     */
    @Override
    public boolean purchaseReview(String equipmentNo) {
        int count = adminReviewMapper.adminPurchaseReview(equipmentNo);

        return count == 0 ? false : true;
    }

    /**
     * @Description 运行使用审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   boolean
     * @params  [equipmentNo, beginDate]
     */
    @Override
    public boolean useRecordReview(String equipmentNo, Date beginDate) {
        DateAndEquipmentNoVo dateAndEquipmentNoVo = new DateAndEquipmentNoVo();
        dateAndEquipmentNoVo.setDate(beginDate);
        dateAndEquipmentNoVo.setEquipmentNo(equipmentNo);
        int count = adminReviewMapper.adminUseRecordReview(dateAndEquipmentNoVo);

        return count == 0 ? false : true;
    }

    /**
     * @Description 维护维修审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   boolean
     * @params  [equipmentNo, beginDate]
     */
    @Override
    public boolean repairReview(String equipmentNo, Date beginDate) {
        DateAndEquipmentNoVo dateAndEquipmentNoVo = new DateAndEquipmentNoVo();
        dateAndEquipmentNoVo.setDate(beginDate);
        dateAndEquipmentNoVo.setEquipmentNo(equipmentNo);
        int count = adminReviewMapper.adminRepairReview(dateAndEquipmentNoVo);

        return count == 0 ? false : true;
    }

    // -------------------批量-------------------
    /**
     * @Description 批量损废审核
     * @author mzy
     * @time 2018-12-04
     * @see
     * @return   void
     * @params  [equipmentNo]
     */
    @Override
    public void damageReviewMore(List<String> equipmentNo) {
        for (String e : equipmentNo) {
            adminReviewMapper.adminDamageReview(e.trim());
        }
    }

    /**
     * @Description 批量调入审核
     * @author mzy
     * @time 2018-12-04
     * @see
     * @return   void
     * @params  [equipmentNo]
     */
    @Override
    public void transferInReviewMore(List<String> equipmentNo) {
        for (String e : equipmentNo) {
            adminReviewMapper.adminTransferInReview(e.trim());
        }

    }

    /**
     * @Description 批量调出审核
     * @author mzy
     * @time 2018-12-04
     * @see
     * @return   void
     * @params  [equipmentNo]
     */
    @Override
    public void transferOutReviewMore(List<String> equipmentNo) {
        for (String e : equipmentNo) {
            adminReviewMapper.adminTransferOutReview(e.trim());
        }
    }

    /**
     * @Description 批量购入审核
     * @author mzy
     * @time 2018-12-04
     * @see
     * @return   void
     * @params  [equipmentNo]
     */
    @Override
    public void purchaseReviewMore(List<String> equipmentNo) {
        for (String e : equipmentNo) {
            adminReviewMapper.adminPurchaseReview(e.trim());
        }
    }

    /**
     * @Description 批量运行使用审核
     * @author mzy
     * @time 2018-12-04
     * @see
     * @return   void
     * @params  [dateAndEquipmentNoVos]
     */
    @Override
    public void useRecordReviewMore(List<DateAndEquipmentNoVo> dateAndEquipmentNoVos) throws DateNullException {
        for (DateAndEquipmentNoVo dateAndEquipmentNoVo : dateAndEquipmentNoVos) {
            // 保证时间不为空
            if (dateAndEquipmentNoVo.getDate() == null) {
                throw new DateNullException();
            }
            // ---去个空---
            dateAndEquipmentNoVo.setEquipmentNo(dateAndEquipmentNoVo.getEquipmentNo().trim());
            // -----------
            adminReviewMapper.adminUseRecordReview(dateAndEquipmentNoVo);
        }
    }

    /**
     * @Description 批量维修维护审核
     * @author mzy
     * @time 2018-12-04
     * @see
     * @return   void
     * @params  [dateAndEquipmentNoVos]
     */
    @Override
    public void repairReviewMore(List<DateAndEquipmentNoVo> dateAndEquipmentNoVos) throws DateNullException {
        for (DateAndEquipmentNoVo dateAndEquipmentNoVo : dateAndEquipmentNoVos) {
            // 保证时间不为空
            if (dateAndEquipmentNoVo.getDate() == null) {
                throw new DateNullException();
            }
            // ---去个空---
            dateAndEquipmentNoVo.setEquipmentNo(dateAndEquipmentNoVo.getEquipmentNo().trim());
            // -----------
            adminReviewMapper.adminRepairReview(dateAndEquipmentNoVo);
        }
    }
}
